﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Draco.OWF.RT;
using Draco.OWF.DataBinding;
using Draco.OWF.RT.Engine;

namespace Draco.OWF.RT
{
    public abstract class AbstractJob : IJob
    {
        protected String m_Id = Guid.NewGuid().ToString();
        protected String m_Name;
        protected IProcess m_processTemplate;
        protected IBindingDataItemCollection m_startParameters;
        protected WFProcessStatus m_Status = WFProcessStatus.Inactive;
        protected List<ITask> m_ActivityInstances = new List<ITask>();
        protected List<IRouteInstance> m_RouteInstances = new List<IRouteInstance>();

        #region constructor
        /// <summary>
        /// constructor
        /// </summary>
        /// <param name="processTemplate"></param>
        /// <param name="startParameters"></param>
        public AbstractJob(IProcess processTemplate, IBindingDataItemCollection startParameters)
        {
            if (processTemplate == null)
                throw new ArgumentNullException("processTemplate is null");
            this.m_processTemplate = processTemplate;
            this.m_startParameters = startParameters;
        }
        #endregion

        #region property
        /// <summary>
        /// get or set Id
        /// </summary>
        public string Id
        {
            get { return m_Id; }
            set
            {
                if (!String.IsNullOrEmpty(value))
                    this.m_Id = value;
            }
        }
        /// <summary>
        /// get or set Name
        /// </summary>
        public string Name
        {
            get { return m_Name; }
            set { m_Name = value; }
        }
        /// <summary>
        /// 
        /// </summary>
        public virtual WFProcessStatus Status
        {
            get { return m_Status; }
            set { m_Status = value; }
        }

        public virtual IProcess WorkflowDefine
        {
            get { return m_processTemplate; }
        }
        #endregion

        /// <summary>
        /// 
        /// </summary>
        public virtual void Initialize()
        {
        }

        public void AddActivityInstance(ITask acIns)
        {
            if (acIns == null)
                throw new ArgumentNullException();
            ITask tar = GetActivityInstance(acIns.Id);
            if (tar == null)
            {
                this.m_ActivityInstances.Add(acIns);
            }
        }

        public void AddRouteInstance(IRouteInstance routeIns)
        {
            if (routeIns == null)
                throw new ArgumentNullException();
            IRouteInstance tar = GetRouteInstance(routeIns.Id);
            if (tar == null)
            {
                this.m_RouteInstances.Add(routeIns);
            }
        }


        public ITask GetActivityInstance(string AcInsId)
        {
            foreach (var ac in this.m_ActivityInstances)
            {
                if (ac.Id == AcInsId)
                    return ac;
            }
            return null;
        }

        public IRouteInstance GetRouteInstance(string routeInsId)
        {
            foreach (var r in this.m_RouteInstances)
            {
                if (r.Id == routeInsId)
                    return r;
            }
            return null;
        }
    }
}
